package com.marlsite.datadrivendeco.primitives;

import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;

import java.util.List;
import java.util.function.Consumer;

import javax.annotation.Nonnull;

import com.marlsite.datadrivendeco.DataDrivenDeco;
import com.marlsite.datadrivendeco.data.PrimitiveDataLoader;
import com.marlsite.datadrivendeco.data.PrimitiveDefinition;
import com.marlsite.datadrivendeco.renderer.BlockEntityWithoutLevelRenderer;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;

public class ItemPrimitive extends Item {
    public ItemPrimitive(Item.Properties properties) {
        super(properties);
    }

    @Override
    public Component getName(@Nonnull ItemStack stack) {
        PrimitiveDefinition primitiveDefinition = getPrimitiveDefinition(stack);
        if (primitiveDefinition != null) {
            if (primitiveDefinition.nameIsTranslatable) {
                return Component.translatable(primitiveDefinition.name);
            } else {
                return Component.literal(primitiveDefinition.name);
            }
        }
        return Component.translatable(getDescriptionId(stack));
    }
    
    private PrimitiveDefinition getPrimitiveDefinition(ItemStack stack) {
        CompoundTag nbt = stack.getOrCreateTag();
        if (nbt.contains("DataDrivenDecoItemID")) {
            String itemID = nbt.getString("DataDrivenDecoItemID");
            List<PrimitiveDefinition> definitions = PrimitiveDataLoader.getItemDefinitions();
            for (PrimitiveDefinition definition : definitions) {
                if (definition.id.equals(itemID)) {
                    return definition;
                }
            }
        }
        return null;
    }


    @Override
    public void initializeClient(@Nonnull Consumer<IClientItemExtensions> consumer) {
        consumer.accept(new IClientItemExtensions() {

            @Override
            public BlockEntityWithoutLevelRenderer getCustomRenderer() {
            return DataDrivenDeco.BEWLR;
            }
        });
    }
}
